--累计充值

local Msg = require("core.Msg")
local SceneHandler = require("scene.Handler")
local CommonDB = require("common.CommonDB")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local LeijiChongzhiExcel = require("excel.leijiChongzhi")

initFlag = false
local function getConfig()
	local now = os.time()
	if not initFlag then
		for k,v in pairs(LeijiChongzhiExcel.define) do
			v.startDate.hour = 0
			v.startDate.min = 0
			v.startDate.sec = 0
			v.startTime = os.time(v.startDate)
			v.endDate.hour = 0
			v.endDate.min = 0
			v.endDate.sec = 0
			v.endTime = os.time(v.endDate) + 86400
		end
		initFlag = true
	end

	for k,v in pairs(LeijiChongzhiExcel.define) do
		if now >= v.startTime and now <= v.endTime then
			return LeijiChongzhiExcel[v.confID],v.startTime,v.endTime
		end
	end
end

local function checkDB(human)
	local leichongExcel,startTime,endTime = getConfig()
	if not leichongExcel then
		return
	end
	human.db.leijiChongzhi = human.db.leijiChongzhi or {startTime = 0,endTime = 0,get = {},cnt = 0}
	local leichongDB = human.db.leijiChongzhi
	if leichongDB.startTime ~= startTime or leichongDB.endTime ~= endTime then
		leichongDB.startTime = startTime
		leichongDB.endTime = endTime
		leichongDB.get = {}
		leichongDB.cnt = 0
	end
	return leichongExcel,leichongDB
end

STATE0 = 0	--不可领取
STATE1 = 1	--未领取
STATE2 = 2	--已经领取

local function getState(leichongDB,id,need)
	if leichongDB.cnt < need then
		return STATE0
	end
	if leichongDB.get[id] then
		return STATE2
	else
		return STATE1
	end
end

function query(human)
	local leichongExcel,leichongDB = checkDB(human)
	if not leichongExcel then
		assert()
	end
	local msgRet = Msg.gc.GC_LEIJICHONGZHI_QUERY
	local len = 0
	local len1
	for k,v in ipairs(leichongExcel) do
		len = len + 1
		local net = msgRet.list[len]
		net.id = k
		net.maxCnt = v.maxCnt
		len1 = 0
		for k1,v1 in ipairs(v.items) do
			len1 = len1 + 1
			Grid.makeItem(net.reward[len1], v1[1], v1[2])
		end
		net.reward[0] = len1
		net.state = getState(leichongDB,k,v.cnt)
	end
	msgRet.list[0] = len
end

function get(human,id)
	local leichongExcel,leichongDB = checkDB(human)
	if not leichongExcel then
		assert()
	end
	local conf = leichongExcel[id]
	if getState(leichongDB,id,conf.cnt) ~= STATE1 then
		return
	end

	leichongDB.get[id] = 1

	--发放奖励 
	BagLogic.sendItemGetList(human, conf.items, "leijiChongzhi")

	local msgRet = Msg.gc.GC_LEIJICHONGZHI_GET
	msgRet.id = id
	Msg.send(msgRet,human.fd)
end

function isOpen(human)
	if true then
		return
	end

	if not SceneHandler.canCharge(human) then
		return
	end

	if not CommonDB.getServerOpenDay() then
		return 
	end
	local leichongExcel = getConfig()
	if not leichongExcel then
		return
	end
	return true
end


function isRed(human)
	local leichongExcel,leichongDB = checkDB(human)
	if not leichongExcel then return end
	for k,v in ipairs(leichongExcel) do
		if getState(leichongDB,k,v.cnt) == STATE1 then
			return true
		end
	end
end

function onAddMoney(human,money)
	if isOpen(human) ~= true then
		return
	end

	local leichongExcel,leichongDB = checkDB(human)
	if not leichongExcel then
		return
	end	
	leichongDB.cnt = leichongDB.cnt + money
	for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3303)
        break
    end
end